package com.zygame.sdk.bean;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.zygame.sdk.utils.SharedPreferencesHelper;
import com.zygame.sdk.utils.log.LogHelper;

@SuppressWarnings("unchecked")
public class ConfigMessage {
    private static ConfigMessage configMessage;

    private ArrayList<ZY_Toolbar> main;
    private ArrayList<Toolbar> toolbar;
    private ArrayList<Pay> pay;
    private List<String> payList;

    public static String configVersion;
    public static String iconsUrl;

    // 联系信息
    public static String website;
    public static String qq;
    public static String phone;

    private final static String TAG = "ConfigMessage";

    private final static String MAIN = "main";
    private final static String ID = "id";
    private final static String PID = "pid";
    private final static String NAME = "name";
    private final static String ACTION = "action";
    private final static String SRC = "src";
    private final static String TYPE = "type";
    private final static String AUTH = "auth";
    private final static String URL = "url";
    private final static String POS = "pos";
    private final static String TOOLBAR = "tool_bar";
    private final static String HIDE = "hide";
    private final static String SEQ = "seq";
    private final static String STATUS = "status";

    private final static String SUBMENU = "submenu";
    private final static String NAVIBUTTONS = "navibuttons";
    private final static String PAY = "pay";
    private final static String CONTACT_MENU = "contact_menu";

    private ConfigMessage() {
    }

    public static ConfigMessage getInstance() {
        if (configMessage == null) {
            synchronized (ConfigMessage.class) {
                if (configMessage == null) {
                    configMessage = new ConfigMessage();
                }
            }
        }
        return configMessage;
    }

    /**
     * 解析json
     * 1
     *
     * @param jsonObj
     * @param mContext
     */
    public void jsonParse(JSONObject jsonObj, Context mContext) {

//		JSONObject mainObj = jsonObj.optJSONObject(MAIN);
//		this.main = parse(mainObj, MAIN);

//		JSONObject toolbarObj = jsonObj.optJSONObject(TOOLBAR);
        this.main = parse(jsonObj, TOOLBAR);
        payList = parseType(jsonObj, mContext);

//		JSONObject payObj = jsonObj.optJSONObject(PAY);
//		this.pay = parse(payObj, PAY);
//		parseOther(jsonObj);

//		JSONObject contactMenu = jsonObj.optJSONObject(CONTACT_MENU);
//		parseContact(contactMenu);
    }

    private List<String> parseType(JSONObject jsonObj, Context mContext) {
        JSONArray jsonArray = jsonObj.optJSONArray("type");
        List<String> mList = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.optJSONObject(i);
            String name = jsonObject.optString("name");
            String type = jsonObject.optString("type");
            SharedPreferencesHelper sp = SharedPreferencesHelper.getInstance();

            if (name.equals("GMail")) {
                sp.setGMail(mContext, type);
            } else if (name.equals("Wechat")) {
                sp.setWechat(mContext, type);
            }
            mList.add(name);
        }

        return mList;

    }

    /**
     * 获取main数据
     *
     * @return
     */
    public List<ZY_Toolbar> getMainData() {
        return this.main;
    }
    /**
     * 获取main数据
     *
     * @return
     */
    public List<String> getPayListData() {
        return this.payList;
    }

    /**
     * 获取toolbar数据
     *
     * @return
     */
    public List<Toolbar> getToolbarData() {
        return this.toolbar;
    }

    /**
     * 获取pay数据
     *
     * @return
     */
    public List<Pay> getPayData() {
        return this.pay;
    }

    /**
     * 解析ver、icons
     */
    private static void parseOther(JSONObject jsonObj) {
        try {
            configVersion = jsonObj.optString("ver");
            iconsUrl = jsonObj.optString("icons");
        } catch (Exception e) {
            LogHelper.i(TAG, "解析数据失败");
            e.printStackTrace();
        }
    }

    private static void parseContact(JSONObject jsonObj) {
        try {
            website = jsonObj.optString("website");
            qq = jsonObj.optString("qq");
            phone = jsonObj.optString("phone");
        } catch (Exception e) {
            LogHelper.i(TAG, "解析数据失败");
            e.printStackTrace();
        }
    }

    /**
     * 解析json
     */
    @SuppressWarnings("rawtypes")
    private static ArrayList parse(JSONObject jsonObj, String parseName) {
        ArrayList list = new ArrayList();
        try {
            JSONArray jsonArray = jsonObj.optJSONArray(TOOLBAR);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.optJSONObject(i);
                String name = jsonObject.optString("name");
                String action = jsonObject.optString("action");
                String src = jsonObject.optString("src");
                String url = jsonObject.optString("url");
                String hide = jsonObject.optString("hide");
                ZY_Toolbar cd = new ZY_Toolbar(name, action, src, url, hide);

                list.add(cd);
            }


        } catch (Exception e) {

        }
        /*try {
            // JSONObject mainObj = jsonObj.optJSONObject(MAIN);
			JSONArray subArray1 = jsonObj.optJSONArray(SUBMENU); // 第一层sunmenu
			for (int i = 0; i < subArray1.length(); i++) {
				JSONObject item = subArray1.optJSONObject(i);
				String id = item.optString(ID);
				String pid = item.optString(PID);
				String name = item.optString(NAME);
				String action = item.optString(ACTION);
				String src = item.optString(SRC);
				String type = item.optString(TYPE);
				String auth = item.optString(AUTH);
				String url = item.optString(URL);
				String pos = item.optString(POS);
				String toolbar = item.optString(TOOLBAR);
				String hide = item.optString(HIDE);
				String seq = item.optString(SEQ);
				String status = item.optString(STATUS);

				// 解析submenu
				JSONArray subArray2 = item.optJSONArray(SUBMENU); // 第二层sunmenu
				ArrayList<Submenu> subList2 = new ArrayList<Submenu>();
				if (subArray2 != null) {
					for (int j = 0; j < subArray2.length(); j++) {
						JSONObject item2 = subArray2.getJSONObject(j);
						String su_id2 = item2.optString(ID);
						String su_pid2 = item2.optString(PID);
						String su_name2 = item2.optString(NAME);
						String su_action2 = item2.optString(ACTION);
						String su_src2 = item2.optString(SRC);
						String su_type2 = item2.optString(TYPE);
						String su_auth2 = item2.optString(AUTH);
						String su_url2 = item2.optString(URL);
						String su_pos2 = item2.optString(POS);
						String su_toolbar2 = item2.optString(TOOLBAR);
						String su_hide2 = item2.optString(HIDE);
						String su_seq2 = item2.optString(SEQ);
						String su_status2 = item2.optString(STATUS);

						// 解析submenu
						JSONArray subArray3 = item2.optJSONArray(SUBMENU); // 第三层sunmenu
						ArrayList<Submenu> subList3 = new ArrayList<Submenu>();
						if (subArray3 != null) {
							for (int k = 0; k < subArray3.length(); k++) {
								JSONObject item3 = subArray3.optJSONObject(k);
								String su_id3 = item3.optString(ID);
								String su_pid3 = item3.optString(PID);
								String su_name3 = item3.optString(NAME);
								String su_action3 = item3.optString(ACTION);
								String su_src3 = item3.optString(SRC);
								String su_type3 = item3.optString(TYPE);
								String su_auth3 = item3.optString(AUTH);
								String su_url3 = item3.optString(URL);
								String su_pos3 = item3.optString(POS);
								String su_toolbar3 = item3.optString(TOOLBAR);
								String su_hide3 = item3.optString(HIDE);
								String su_seq3 = item3.optString(SEQ);
								String su_status3 = item3.optString(STATUS);

								Submenu submenu = new Submenu(su_id3, su_pid3, su_name3, su_action3, su_src3, su_type3, su_auth3, su_url3, su_pos3, su_toolbar3,su_hide3, su_seq3, su_status3, null, null);
								subList3.add(submenu);
							}
						}

						// 解析navibuttons
						JSONArray navArray3 = item2.optJSONArray(NAVIBUTTONS); // 第三层navibuttons
						ArrayList<Navibutton> navList3 = new ArrayList<Navibutton>();
						if (navArray3 != null) {
							for (int k = 0; k < navArray3.length(); k++) {
								JSONObject item3 = navArray3.optJSONObject(k);
								String su_id3 = item3.optString(ID);
								String su_pid3 = item3.optString(PID);
								String su_name3 = item3.optString(NAME);
								String su_action3 = item3.optString(ACTION);
								String su_src3 = item3.optString(SRC);
								String su_type3 = item3.optString(TYPE);
								String su_auth3 = item3.optString(AUTH);
								String su_url3 = item3.optString(URL);
								String su_pos3 = item3.optString(POS);
								String su_toolbar3 = item3.optString(TOOLBAR);
								String su_hide3 = item3.optString(HIDE);
								String su_seq3 = item3.optString(SEQ);
								String su_status3 = item3.optString(STATUS);

								Navibutton navibutton = new Navibutton(su_id3, su_pid3, su_name3, su_action3, su_src3, su_type3, su_auth3, su_url3, su_pos3, su_toolbar3,su_hide3, su_seq3, su_status3, null,
										null);
								navList3.add(navibutton);
							}
						}

						Submenu submenu = new Submenu(su_id2, su_pid2, su_name2, su_action2, su_src2, su_type2, su_auth2, su_url2, su_pos2, su_toolbar2,su_hide2, su_seq2, su_status2, subList3, navList3);
						subList2.add(submenu);
					}
				}

				// 解析navibuttons
				JSONArray navArray2 = item.optJSONArray(NAVIBUTTONS); // 第二层navibuttons
				ArrayList<Navibutton> navList2 = new ArrayList<Navibutton>();
				if (navList2 != null) {
					for (int j = 0; j < navArray2.length(); j++) {
						JSONObject item2 = navArray2.optJSONObject(j);
						String nav_id2 = item2.optString(ID);
						String nav_pid2 = item2.optString(PID);
						String nav_name2 = item2.optString(NAME);
						String nav_action2 = item2.optString(ACTION);
						String nav_src2 = item2.optString(SRC);
						String nav_type2 = item2.optString(TYPE);
						String nav_auth2 = item2.optString(AUTH);
						String nav_url2 = item2.optString(URL);
						String nav_pos2 = item2.optString(POS);
						String nav_toolbar2 = item2.optString(TOOLBAR);
						String nav_hide2 = item2.optString(HIDE);
						String nav_seq2 = item2.optString(SEQ);
						String nav_status2 = item2.optString(STATUS);

						// **************************************************
						// 解析submenu
						JSONArray subArray3 = item2.optJSONArray(SUBMENU); // 第三层sunmenu
						ArrayList<Submenu> subList3 = new ArrayList<Submenu>();
						if (subArray3 != null) {
							for (int k = 0; k < subArray3.length(); k++) {
								JSONObject item3 = subArray3.optJSONObject(k);
								String su_id3 = item3.optString(ID);
								String su_pid3 = item3.optString(PID);
								String su_name3 = item3.optString(NAME);
								String su_action3 = item3.optString(ACTION);
								String su_src3 = item3.optString(SRC);
								String su_type3 = item3.optString(TYPE);
								String su_auth3 = item3.optString(AUTH);
								String su_url3 = item3.optString(URL);
								String su_pos3 = item3.optString(POS);
								String su_toolbar3 = item3.optString(TOOLBAR);
								String su_hide3 = item3.optString(HIDE);
								String su_seq3 = item3.optString(SEQ);
								String su_status3 = item3.optString(STATUS);

								Submenu submenu = new Submenu(su_id3, su_pid3, su_name3, su_action3, su_src3, su_type3, su_auth3, su_url3, su_pos3, su_toolbar3,su_hide3, su_seq3, su_status3, null, null);
								subList3.add(submenu);
							}
						}

						// 解析navibuttons
						JSONArray navArray3 = item2.optJSONArray(NAVIBUTTONS); // 第三层navibuttons
						ArrayList<Navibutton> navList3 = new ArrayList<Navibutton>();
						if (navArray3 != null) {
							for (int k = 0; k < navArray3.length(); k++) {
								JSONObject item3 = navArray3.optJSONObject(k);
								String su_id3 = item3.optString(ID);
								String su_pid3 = item3.optString(PID);
								String su_name3 = item3.optString(NAME);
								String su_action3 = item3.optString(ACTION);
								String su_src3 = item3.optString(SRC);
								String su_type3 = item3.optString(TYPE);
								String su_auth3 = item3.optString(AUTH);
								String su_url3 = item3.optString(URL);
								String su_pos3 = item3.optString(POS);
								String su_toolbar3 = item3.optString(TOOLBAR);
								String su_hide3 = item3.optString(HIDE);
								String su_seq3 = item3.optString(SEQ);
								String su_status3 = item3.optString(STATUS);

								Navibutton navibutton = new Navibutton(su_id3, su_pid3, su_name3, su_action3, su_src3, su_type3, su_auth3, su_url3, su_pos3, su_toolbar3,su_hide3, su_seq3, su_status3, null,
										null);
								navList3.add(navibutton);
							}
						}
						// **************************************************

						Navibutton navibuttons = new Navibutton(nav_id2, nav_pid2, nav_name2, nav_action2, nav_src2, nav_type2, nav_auth2, nav_url2, nav_pos2, nav_toolbar2,nav_hide2, nav_seq2, nav_status2,
								subList3, navList3);
						navList2.add(navibuttons);
					}
				}

				// ConfigData cd = null;
				if (MAIN.equals(parseName)) {
					Main cd = new Main(id, pid, name, action, src, type, auth, url, pos, toolbar,hide, seq, status, subList2, navList2);
					list.add(cd);
				} else if (TOOLBAR.equals(parseName)) {
					Toolbar cd = new Toolbar(id, pid, name, action, src, type, auth, url, pos, toolbar,hide, seq, status, subList2, navList2);
					list.add(cd);
				} else if (PAY.equals(parseName)) {
					Pay pay = new Pay(id, pid, name, action, src, type, auth, url, pos, toolbar,hide, seq, status, subList2, navList2);
					list.add(pay);
				}

			}
		} catch (Exception e) {
			LogHelper.i(TAG, "解析数据失败");
			e.printStackTrace();
		}*/
        return list;
    }

}
